﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;
using Avatar.MembershipService;
using Avatar.Common;

public partial class UserControl_Company_NewResume : System.Web.UI.UserControl
{
    string _key = string.Empty;
    int _page = 0;
    List<int> _industry = new List<int>();
    int _position;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using(var _adc=DBHelper.GetDataContext())
            {
                var _positions = _adc.Position.Where(p => p.FatherID == 0).ToList();
                var _p=new Position();
                _p.ID=0;
                _p.Name="不限";
                _p.FatherID=0;
                _positions.Add(_p);
                ddl_Position.DataSource = _positions.OrderBy(p => p.ID);
                ddl_Position.DataBind();
            }
        }
        _key = Request.QueryString["key"] == null ? "" : Server.UrlDecode(Request.QueryString["key"].ToString());
        _page = Request.QueryString["page"] == null ? 0 : Convert.ToInt32(Request.QueryString["page"].ToString());

        if (Request.QueryString["industry"] != null)
        {
            var _industryStr = Request.QueryString["industry"].ToString().Split(new char[] { ',' });
            _industry = new List<int>();

            foreach (var _i in _industryStr)
            {
                if (!string.IsNullOrEmpty(_i.Trim()))
                {
                    _industry.Add(Convert.ToInt32(_i));
                }
            }
        }

        if (Request.QueryString["position"] != null)
        {
            _position = Convert.ToInt32(Request.QueryString["position"].ToString());
        }

        this.Search(_page, _key, _industry, _position);
    }
    protected void btn_View_Vip(object sender, EventArgs e)
    {
        Button _btn = sender as Button;

        Page.RegisterClientScriptBlock("resume", "<script>window.open('resumeview.aspx?user=" + (string.IsNullOrEmpty(_btn.CommandArgument) ? string.Empty : _btn.CommandArgument) + "','_blank');</script>");
    }
    protected void btn_View_General(object sender, EventArgs e)
    {

    }
    protected void btn_View_Monthly(object sender, EventArgs e)
    {

    }
    protected void btn_Up_Click(object sender, EventArgs e)
    {
        int _page = 0;
        _page = Convert.ToInt32(lbl_Current.Text) - 1;
        if (_page > 0) _page -= 1;
        string _industryStr = "";
        if (_industry != null && _industry.Count() > 0)
        {
            foreach (var _i in _industry)
            {
                _industryStr += _i.ToString() + ",";
            }
        }

        Response.Redirect("./CompanyHome.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + _page + "&Industry=" + _industryStr + "&Position=" + _position.ToString());
    }
    protected void btn_Down_Click(object sender, EventArgs e)
    {
        int _page = 0;
        _page = Convert.ToInt32(lbl_Current.Text) - 1;
        if (_page < Convert.ToInt32(lbl_Total.Text) - 1) _page += 1;
        string _industryStr = "";
        if (_industry != null && _industry.Count() > 0)
        {
            foreach (var _i in _industry)
            {
                _industryStr += _i.ToString() + ",";
            }
        }

        Response.Redirect("./CompanyHome.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + _page + "&Industry=" + _industryStr + "&Position=" + _position.ToString());
    }
    protected void btn_Goto_Click(object sender, EventArgs e)
    {
        int _page = 0;
        try
        {
            _page = Convert.ToInt32(txt_Page.Text) - 1;
            if (_page < 0 || _page > Convert.ToInt32(lbl_Total.Text) - 1)
            {
                _page = Convert.ToInt32(lbl_Current.Text) - 1;
            }
        }
        catch
        {
            _page = Convert.ToInt32(lbl_Current.Text) - 1;
        }

        string _industryStr = "";
        if (_industry != null && _industry.Count() > 0)
        {
            foreach (var _i in _industry)
            {
                _industryStr += _i.ToString() + ",";
            }
        }

        Response.Redirect("./CompanyHome.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + _page + "&Industry=" + _industryStr + "&Position=" + _position.ToString());
    }
    protected void Search(int _page, string _key, List<int> _industry, int _position)
    {
        int _take = 15;
        using (var _adc = DBHelper.GetDataContext())
        {
            lbl_Current.Text = (_page + 1).ToString();

            IQueryable<Resume> _resume = null;
            //000
            if (string.IsNullOrEmpty(_key) && _industry.Count() < 1 && _position <1)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty)/_take).ToString();

            }///001
            else if (string.IsNullOrEmpty(_key) && _industry.Count() < 1 && _position > 0)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.PositionID == _position).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.PositionID == _position) / _take).ToString();
            }///100
            else if (!string.IsNullOrEmpty(_key) && _industry.Count() < 1 && _position < 1)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && (r.PersonalBasicInfo.KeyWord.Contains(_key) || r.JobIntention.PositionName.Contains(_key))).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && (r.PersonalBasicInfo.KeyWord.Contains(_key) || r.JobIntention.PositionName.Contains(_key))) / _take).ToString();
            }///101
            else if (!string.IsNullOrEmpty(_key) && _industry.Count() < 1 && _position > 0)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && (r.PersonalBasicInfo.KeyWord.Contains(_key) || r.JobIntention.PositionName.Contains(_key)) && r.JobIntention.PositionID == _position).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && (r.PersonalBasicInfo.KeyWord.Contains(_key) || r.JobIntention.PositionName.Contains(_key)) && r.JobIntention.PositionID == _position) / _take).ToString();
            }///010
            else if (string.IsNullOrEmpty(_key) && _industry.Count() > 0 && _position < 1)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0) / _take).ToString();
            }///011
            else if (string.IsNullOrEmpty(_key) && _industry.Count() > 0 && _position > 0)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0 && r.JobIntention.PositionID==_position).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0 && r.JobIntention.PositionID==_position) / _take).ToString();
            }///110
            else if (!string.IsNullOrEmpty(_key) && _industry.Count() > 0 && _position < 1)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0 && (r.PersonalBasicInfo.KeyWord.Contains(_key) || r.JobIntention.PositionName.Contains(_key))).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0 && r.PersonalBasicInfo.KeyWord.Contains(_key)) / _take).ToString();
            }///111
            else if (!string.IsNullOrEmpty(_key) && _industry.Count() > 0 && _position > 0)
            {
                _resume = _adc.Resume.Where(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0 && (r.PersonalBasicInfo.KeyWord.Contains(_key) || r.JobIntention.PositionName.Contains(_key)) && r.JobIntention.PositionID == _position).OrderByDescending(r => r.UpdateDate).Skip(_take * _page).Take(_take);
                lbl_Total.Text = (_adc.Resume.Count(r => r.PersonalBasicInfo != null && r.JobIntention != null && r.PersonalBasicInfo.Name != string.Empty && r.JobIntention.JobIntentionIndustry.Where(i => _industry.Contains(i.IndustryID)).Count() > 0 && (r.PersonalBasicInfo.KeyWord.Contains(_key) || r.JobIntention.PositionName.Contains(_key)) && r.JobIntention.PositionID == _position) / _take).ToString();
            }

            this.Bind(_resume);
        }
    }
    protected void Bind(IQueryable<Resume> _resume)
    {
        //Industry1.Select = _industry;
        //PositionII1.Select = _position;

        #region 根据用户身份确定绑定用的GV

        var _user = LoginManager.GetCurrentUser();
        if (_user != null)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _companyService = _adc.CompanyService.FirstOrDefault(c => c.Company == _user.UserName);
                if (_companyService != null)
                {
                    switch (_companyService.ServiceTypeID)
                    {
                        case 1:
                            gv_General.DataSource = _resume;
                            gv_General.DataBind();
                            break;
                        case 2:
                            gv_Vip.DataSource = _resume;
                            gv_Vip.DataBind();
                            break;
                        case 3:
                            gv_Monthly.DataSource = _resume;
                            gv_Monthly.DataBind();
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    gv_General.DataSource = _resume;
                    gv_General.DataBind();
                }
            }
        }
        #endregion
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            _key = txt_Key.Text;
            _industry = Industry1.Select;
            _position = Convert.ToInt32(ddl_Position.SelectedValue);

            string _industryStr = "";
            if (_industry != null && _industry.Count() > 0)
            {
                foreach (var _i in _industry)
                {
                    _industryStr += _i.ToString() + ",";
                }
            }

            Response.Redirect("./CompanyHome.aspx?Key=" + Server.UrlEncode(_key) + "&Page=" + 0 + "&Industry=" + _industryStr + "&Position=" + _position.ToString());
        }
        catch (Exception _e)
        {
            Helper.ShowMassage(_e.Message);
        }
    }
}
